<?php

class Rapide_Log_Adapter_File implements Rapide_Log_Adapter_Interface
{
	private $_rFile;

	public function __construct($sFile)
	{
		if(!is_writable($sFile))
		{
			$sError = sprintf('Log file %s is not writable or does not exist', $sFile);
			throw new Rapide_Log_Adapter_File_Exception($sError);
		}

		$rFile = fopen($sFile, 'a+');

		if(!is_resource($rFile))
		{
			$sError = sprintf('Can not open log file %s', $sFile);
			throw new Rapide_Log_Adapter_File_Exception($sError);
		}

		$this->_rFile = $rFile;
	}

	public function log($sMessage)
	{
		$sTime		= strftime('%x %X', time());
		$sMessage = str_replace("\t", '		', $sMessage);
		$sMessage = str_replace("\n", ' ', $sMessage);

		$sLogLine = $sTime . "\t" . $sMessage . "\n";

		fwrite($this->_rFile, $sLogLine);
	}

	public function __destruct()
	{
		fclose($this->_rFile);
	}
}

?>